package com.alquiler.validations;


import javafx.scene.control.ComboBox;
import javafx.scene.control.ComboBoxBase;
import javafx.scene.control.Control;
import javafx.scene.control.Dialogs;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextInputControl;
import javafx.stage.Stage;

public class Validations {
//	
	
	
	public static boolean isInputValid(Stage stage, Control... fields){		
		
		
		String errorMessage = "";
			
		errorMessage = validateInputsRequerid(fields);		
		
		if (errorMessage.length() == 0) {
            return true;
            
            
        } else {            
            Dialogs.showErrorDialog(stage, errorMessage,
                    "Por favor Corregir los campos invalidos", "Campos Invalidos");
            return false;
        }	
	}	
	
	
	
	
	public static String validateInputsRequerid(Control... fields){
		String message = "";
		
		if(fields.length > 0){
			
			for (Control field : fields) {				
				
				try {					
					if(field instanceof TextInputControl || field instanceof TextArea || field instanceof PasswordField) {	
						
						validateField((TextInputControl) field);	
						
					}else if(field instanceof ComboBox<?>){
						
						validateCombo((ComboBox<?>)field);
						
					};
								
				} catch (Exception e) {
					message += e.getMessage();
				}	
			}      
		}
		
		return message;
	}
	
	
	
	
	private static void validateField(TextInputControl field) throws Exception { 
		
        if (field.getText() == null || field.getText().length() == 0){
            throw new Exception("El campo "+field.getId()+" es requerido!\n");
        }
        
        if(field.getText().length() > 100){        	
        	throw new Exception("El texto del campo "+field.getId()+" es demasiado largo!\n");
        }
       
    }
	
	
	private static void validateCombo(ComboBoxBase<?> field) throws Exception { 
		
        if (field.getValue() == null){
            throw new Exception("El campo "+field.getId()+" es requerido!\n");
        }        
       
    }
	

}
